using SmartBoard.Book.Model;

namespace SmartBoard.Book.Commands
{
    public class OpenBookAction:BookUIActionBase
    {
        public OpenBookAction(BookManager bookManager, IBookUIManager bookUIManager):base(bookManager, bookUIManager)
        {
            
        }

        #region ICommand Members

        public override bool CanExecute(object parameter)
        {
            return true;
        }

        public override void Execute(object parameter)
        {
            if (CanOpenBook())
            {
                var dialog = _bookUIManager.OpenBookUIDialog;
                if (dialog.PresentUI() == true)
                {
                    _bookManager.Book = _bookManager.DataManager.Read(dialog.Location);
                }
            }
        }
        #endregion

        private bool CanOpenBook()
        {
            if (_bookManager.Book == null)
                return true;
            if (_bookManager.Book.IsEmpty)
                return true;
            return _bookUIManager.CanOpenBookUIDialog.PresentUI() == true;
        }
    }
}